package com.chazz.servlet.order;

import com.chazz.dao.OrderDao;
import com.chazz.dao.UserDao;
import com.chazz.dao.impl.OrderDaoImpl;
import com.chazz.dao.impl.UserDaoImpl;
import com.chazz.entity.*;
import com.chazz.utils.JSONUtils;

import com.chazz.utils.TotalPriceUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;
@WebServlet("/ordersave.do")
public class OrderServlet extends HttpServlet {
    private UserDao userDaoImpl = new UserDaoImpl();
    private OrderDao orderDaoImpl = new OrderDaoImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");
        String method = req.getParameter("method");
        if(method==null){
            save(req,resp);
        }else if("add".equals(method)){
            addAdress(req,resp);
        }else if("choice".equals(method)){
            choice(req,resp);
        }
    }

    private void choice(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    private void addAdress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String address = req.getParameter("adress");
        System.out.println(address);
    }

    private void gotocart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String address = req.getParameter("goto");
        resp.sendRedirect("/foraging/order.html");
    }


    private void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //判断是否登录
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        if(user==null){
            //跳转登录界面
            req.getRequestDispatcher("login.do").forward(req,resp);
        }else{
            //获取json类型的订单表
            String orders = req.getParameter("orders");
            String goods = req.getParameter("goods");
            List<GoodsInfo> goodsInfos = JSONUtils.toList(GoodsInfo.class,goods);
            List<Orders> orderList = JSONUtils.toList(Orders.class,orders);
            Double total = 0.0;
            for(GoodsInfo goodsInfo : goodsInfos){
                com.chazz.Do.GoodsInfo info = new com.chazz.Do.GoodsInfo(
                    user.getId(), goodsInfo.getCount(), goodsInfo.getGoodsid());
                double count = new TotalPriceUtils().count(info);
                total+=count;
            }
            //获取订单类
            Orders order = orderList.get(0);
            //将订单信息储存到订单表
            System.out.println(orders);
            System.out.println(goods);
            Date date = new Date();
            order.setId(new UserDaoImpl().findOrderFinalId());
            order.setCreatetime(date);
            order.setOrderadress("成都");
            order.setTatolprice(String.valueOf(total));
            orderDaoImpl.orderAdd(order);
            //再将刚刚存的订单取出，主要获得他的id，方便存放详情表
            Orders o = orderDaoImpl.findByUid(order.getId());
            System.out.println(o.getId());
            //建立订单详情集合，逐个放到数据库 储存订单细节信息（订单中的商品）

            for(com.chazz.entity.GoodsInfo goodsInfo : goodsInfos){
                goodsInfo.setUserid(user.getId());
                Orderdetails orderdetail = new Orderdetails();
                orderdetail.setGid(goodsInfo.getGoodsid());
                orderdetail.setOid(o.getId());
                orderdetail.setCount(goodsInfo.getCount());
                com.chazz.Do.GoodsInfo info = new com.chazz.Do.GoodsInfo(
                    user.getId(), goodsInfo.getCount(), goodsInfo.getGoodsid());
                orderdetail.setTatolprice(String.valueOf(new TotalPriceUtils().count(info)));

                orderDaoImpl.orderdetailsAdd(orderdetail);
            }
            session.setAttribute("order",o);
            List<Adress> adresses = userDaoImpl.findAddressByUid(user.getId());
            //session.setAttribute("adresses",adresses);
            //跳转到选取地址页面，将页面选择后储存到数据库中
            resp.sendRedirect("/foraging/order.html");
        }
    }
}
